package com.stars.controller.system;
import com.stars.common.maps.JsonUtils;
import com.stars.entity.system.SysUrlsEntity;
import com.stars.services.system.SysUrlsAccessServices;
import com.stars.services.system.SysUrlsConfigServices;
import com.stars.entity.dto.system.SysUrlsDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.stars.common.annotation.Log;
import com.stars.common.base.BaseMsg;
import com.stars.common.ObjectCopyUtils;
import com.stars.common.base.BaseConstant;
import com.stars.common.base.ResultMsg;
import com.stars.controller.BaseController;
import com.stars.common.enums.OperationType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;

import com.stars.common.valid.UpdateView;
import com.github.pagehelper.PageInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Date;
import java.util.List;

/**
 * @Title: 系统所有URL管理表 mybatis dao
 * @author dj
 * @date 2019-10-21 12:43:35
 * @version V1.0
 *
 */
@Slf4j
@RestController
@RequestMapping("/sysUrlsCtl")
@Api(tags ="系统所有URL管理表")
public class SysUrlsCtl extends BaseController{
   @Autowired
   private SysUrlsAccessServices  sysUrlsAccessServicesImpl;
   @Autowired
   private SysUrlsConfigServices  sysUrlsConfigServicesImpl;

   /**
   *  分页查询
   * @param pageNum 起始页
   * @param pageSize 大小
   * @return 分页数据
   * @throws Exception
   */
   @GetMapping("/pageList")
   @ApiOperation(value = "系统所有URL管理表"+BaseMsg.MODULAR_PAGE,notes ="系统所有URL管理表"+ BaseMsg.MODULAR_PAGE)
   @Log(detail = "系统所有URL管理表"+ BaseMsg.MODULAR_PAGE,operationType = OperationType.SELECT)
   public PageInfo<SysUrlsDTO> pageSysUrlsList(Integer pageNum, Integer pageSize, SysUrlsDTO dto,HttpServletRequest request) throws Exception{
        setSystemApp(dto,request);
        return sysUrlsAccessServicesImpl.pageList(pageNum,pageSize,dto);

   }

    /**
    * 保存
    * @param jsonStr
    * @return 成功失败
    * @throws Exception
    */
    @PostMapping(value = "/add")
    @ApiOperation(value = "系统所有URL管理表"+ BaseMsg.MODULAR_ADD,notes = "系统所有URL管理表"+BaseMsg.MODULAR_ADD)
    @Log(detail = "系统所有URL管理表"+BaseMsg.MODULAR_ADD,operationType = OperationType.INSERT)
    public ResultMsg addSysUrls(String jsonStr,HttpServletRequest request) throws Exception{
        List<SysUrlsEntity> list = JsonUtils.jsonToList(jsonStr,SysUrlsEntity.class);
        SysUrlsEntity entityName = new SysUrlsEntity();
        setAddBaseApp(entityName,request);
        if(null != list){
            list.forEach(url ->{
                List<SysUrlsDTO> urlByUrl = null;
                try {
                    urlByUrl = sysUrlsAccessServicesImpl.findUrlByUrl(url.getUrlName());
                    //如果存在不添加
                    if(null != urlByUrl && urlByUrl.size() > 0){
                        return;
                    }
                    url.setCreateId(entityName.getCreateId());
                    url.setCreateName(entityName.getCreateName());
                    url.setCompanyId(entityName.getCompanyId());
                    url.setAppId(entityName.getAppId());
                    url.setCreateDate(new Date());
                    sysUrlsConfigServicesImpl.insertSelective(url);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

        }
        return ResultMsg.success();

    }
    /**
    * 更新
    * @param updateEntityDto
    * @return 成功失败
    * @throws Exception
    */
    @PostMapping(value = "/update")
    @ApiOperation(value = "系统所有URL管理表"+ BaseMsg.MODULAR_UPDATE,notes = "系统所有URL管理表"+ BaseMsg.MODULAR_ADD)
    @Log(detail = "系统所有URL管理表"+ BaseMsg.MODULAR_UPDATE,operationType = OperationType.UPDATE)
    public ResultMsg updateSysUrls(@Validated({UpdateView.class}) SysUrlsEntity updateEntityDto,HttpServletRequest request) throws Exception{
        SysUrlsEntity entity = sysUrlsAccessServicesImpl.selectById(updateEntityDto.getId());
        ObjectCopyUtils.copyPropertiesIf(updateEntityDto,entity, BaseConstant.UPDATE);
        setUpdateBase(entity,request);
        return sysUrlsConfigServicesImpl.updateSelectiveById(entity);
    }

    /**
    * 根据id查询
    * @param id
    * @return 查询对象
    * @throws Exception
    */
    @GetMapping(value = "/findById")
    @ApiOperation(value = "系统所有URL管理表"+ BaseMsg.MODULAR_FIND_ID,notes = "系统所有URL管理表"+BaseMsg.MODULAR_FIND_ID)
    @Log(detail = "系统所有URL管理表"+ BaseMsg.MODULAR_FIND_ID,operationType = OperationType.SELECT)
    public ResultMsg findSysUrlsById(String id) throws Exception{
        SysUrlsEntity entityName = sysUrlsAccessServicesImpl.selectById(id);
         return ResultMsg.success(entityName);
    }

     /**
     * 删除
     * @param ids
     * @return 成功失败
     * @throws Exception
     */
     @PostMapping(value = "/delById")
     @ApiOperation(value = "系统所有URL管理表"+BaseMsg.MODULAR_DEL,notes ="系统所有URL管理表"+BaseMsg.MODULAR_DEL)
     @Log(detail ="系统所有URL管理表"+BaseMsg.MODULAR_DEL,operationType = OperationType.DELETE)
     public ResultMsg delSysUrlsById(String ids) throws Exception{
       return sysUrlsConfigServicesImpl.deleteByIds(ids.split(","));
     }

}